*--------------------------------------------------------------; * Selects a 1-in-k systematic random sample from a finite ; * population stored in a SAS data set called FRAME ; *--------------------------------------------------------------; %macro sys(noprint,frame=,setup=,npop=,k=,n=,sample=,seed=); %if %length(&seed) = 0 %then %let seed = %str(0); %if %length(&frame) = 0 %then %let frame = %str(frame); %if %length(&npop) = 0 %then %let npop = %str(npop); %if %length(&sample) = 0 %then %let sample = %str(sample); %if %length(&setup) > 0 %then %do; data &setup; set &setup; %end; %else %do; data temp_; %end; %if %length(&k) = 0 %then %do; k_ = int(&npop/&n + .5); %end; %else %do; k_ = &k; %end; start_ = int(ranuni(&seed)*k_) +1; call symput('start',trim(left(start_))); call symput('ktemp',trim(left(k_))); drop k_ start_; run; data temp_; ktemp_ = &ktemp; start_ = &start; data &sample; set &frame; if _n_ = 1 then set temp_; if _n_ = start_ then do; start_ + ktemp_; output; end; drop start_ ktemp_; %if %length(&noprint) = 0 %then %do; proc print data = &sample; title1 "1-in-&ktemp Systematic Random Sample"; title2 "Output Data Set = &sample"; %end; run; title; %mend sys;